﻿Imports MostRecentlyUsed.Common

Public Class frmMain
    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            If (frmRevisionInfo.IsNewVersion()) Then
                My.Settings.Upgrade()
                My.Settings.Reload()
            End If

            If (My.Settings.StartupOption = Constants.StartupOptions.MAXIMIZED) Then
                WindowState = FormWindowState.Maximized
            Else
                WindowState = FormWindowState.Normal
                ' Pull the startup height and width
                Dim iHeight, iWidth As Integer
                GetStartupHeightAndWidth(iHeight, iWidth)
                Width = iWidth
                Height = iHeight

                CenterToScreen()
            End If

            txtDataDirectory.Text = Helper.GetDataDirectory()

            MRUFiles.DeleteOld(txtDataDirectory.Text, "*.*", 7, False, False)

        Catch oExp As Exception
            If (Not Visible) Then Show()
            Helper.DisplayException(oExp, Me, "Exception During Load")
        End Try
    End Sub

    Public Sub CloseIfNoShift()
        If ((ModifierKeys And Keys.Shift) <> Keys.Shift) Then
            Close()
        End If
    End Sub

    Public ReadOnly Property IsOpenRecentProjectsTabSelected() As Boolean
        Get
            Return TabPageOpenRecentProjects.Equals(TabControlMain.SelectedTab)
        End Get
    End Property

    Public ReadOnly Property IsOpenRecentSSMSProjectsTabSelected() As Boolean
        Get
            Return TabPageOpenRecentSSMSProjects.Equals(TabControlMain.SelectedTab)
        End Get
    End Property

    Public ReadOnly Property IsSearchTFSTabSelected() As Boolean
        Get
            Return TabPageSearchTFS.Equals(TabControlMain.SelectedTab)
        End Get
    End Property

    Public ReadOnly Property IsOpenRecentOfficeItemsTabSelected() As Boolean
        Get
            Return TabPageOpenRecentOffice.Equals(TabControlMain.SelectedTab)
        End Get
    End Property

    Private Sub GetStartupHeightAndWidth(ByRef startupHeight As Integer, ByRef startupWidth As Integer)
        startupHeight = NumericUpDownStartupSizeHeight.Minimum
        startupWidth = NumericUpDownStartupSizeWidth.Minimum

        ' Pull the startup height
        Dim iHeight As Integer = My.Settings.StartupSizeHeight
        If (iHeight >= NumericUpDownStartupSizeHeight.Minimum) And (iHeight <= NumericUpDownStartupSizeHeight.Maximum) Then
            startupHeight = iHeight
        End If

        ' Pull the startup width
        Dim iWidth As Integer = My.Settings.StartupSizeWidth
        If (iWidth >= NumericUpDownStartupSizeWidth.Minimum) And (iWidth <= NumericUpDownStartupSizeWidth.Maximum) Then
            startupWidth = iWidth
        End If
    End Sub

    Private Sub pbCheckForNewVersion_Click(sender As Object, e As EventArgs) Handles pbCheckForNewVersion.Click
        Try
            Dim dtBogusDate As DateTime = DateTime.MinValue
            CheckForNewVersion(dtBogusDate, True)
        Catch oExp As Exception
            Helper.DisplayException(oExp, Me, "Exception Checking For New Version")
        End Try
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        AddVersionNumberToFormTitle(Me, True)
    End Sub

    Private Sub pbSettingsBrowse_Click(sender As Object, e As EventArgs) Handles pbSettingsBrowse.Click
        Try
            If m_oOpenFileDialog.ShowDialog() = DialogResult.OK Then
                txtTextEditorPath.Text = m_oOpenFileDialog.FileName
            End If
        Catch oExp As Exception
            Helper.DisplayException(oExp, Me, "Exception Selecting Custom Text Editor")
        End Try
    End Sub

    Private Sub StartupSizeOptionChanged(sender As Object, e As EventArgs) Handles optStartupSizeCustom.CheckedChanged
        Try
            NumericUpDownStartupSizeHeight.Enabled = optStartupSizeCustom.Checked
            NumericUpDownStartupSizeWidth.Enabled = optStartupSizeCustom.Checked
            lblStartupHeight.Enabled = optStartupSizeCustom.Checked
            lblStartupWidth.Enabled = optStartupSizeCustom.Checked
        Catch oExp As Exception
            Helper.DisplayException(oExp, Me, "Exception Setting Startup Size")
        End Try
    End Sub

    Private Sub TabControlMain_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControlMain.SelectedIndexChanged
        Try
            ' This is called from form load as well
            If (TabControlMain.SelectedTab.Equals(TabPageSettings)) Then LoadSettingsTabPage()
        Catch oExp As Exception
            Helper.DisplayException(oExp, Me, "Exception Changing Tabs")
        End Try
    End Sub

    Private Sub LoadSettingsTabPage()
        If ((My.Settings.TextEditorPath <> "C:\WINDOWS\notepad.exe") AndAlso (Not File.Exists(My.Settings.TextEditorPath))) Then
            My.Settings.TextEditorPath = "C:\WINDOWS\notepad.exe"
        End If

        If (String.IsNullOrWhiteSpace(My.Settings.WorkingDirectory) OrElse (Not Directory.Exists(My.Settings.WorkingDirectory))) Then
            Dim szValue As String = Environment.GetEnvironmentVariable("Temp", EnvironmentVariableTarget.User)
            If (String.IsNullOrWhiteSpace(szValue) OrElse (Not Directory.Exists(szValue))) Then _
                szValue = Environment.GetEnvironmentVariable("Temp", EnvironmentVariableTarget.Machine)
            If (String.IsNullOrWhiteSpace(szValue) OrElse (Not Directory.Exists(szValue))) Then szValue = Environment.GetEnvironmentVariable("Tmp", EnvironmentVariableTarget.User)
            If (String.IsNullOrWhiteSpace(szValue) OrElse (Not Directory.Exists(szValue))) Then _
                szValue = Environment.GetEnvironmentVariable("Tmp", EnvironmentVariableTarget.Machine)
            If (String.IsNullOrWhiteSpace(szValue) OrElse (Not Directory.Exists(szValue))) Then szValue = "C:\"
            szValue = Path.Combine(szValue, "MRU")

            My.Settings.WorkingDirectory = szValue
        End If

        txtTextEditorPath.Text = My.Settings.TextEditorPath
        txtWorkingDirectory.Text = My.Settings.WorkingDirectory

        ' Pull the startup height and width
        Dim iHeight, iWidth As Integer
        GetStartupHeightAndWidth(iHeight, iWidth)

        NumericUpDownStartupSizeHeight.Value = iHeight
        NumericUpDownStartupSizeWidth.Value = iWidth

        If (My.Settings.StartupOption = Constants.StartupOptions.MAXIMIZED) Then
            optStartupSizeMaximized.Checked = True
        Else
            optStartupSizeCustom.Checked = True
        End If
    End Sub

    Private Sub pbSettingsSave_Click(sender As Object, e As EventArgs) Handles pbSettingsSave.Click
        Try
            If (Not File.Exists(txtTextEditorPath.Text)) Then
                Helper.DisplayMessage(Me, String.Format("The file, '{0}', does not exist.", txtTextEditorPath.Text), "Text editor does not exist")
                Return
            End If

            Dim szWorkingDirectory As String = txtWorkingDirectory.Text.Trim()
            If (Not Directory.Exists(szWorkingDirectory)) Then Directory.CreateDirectory(szWorkingDirectory)
            My.Settings.WorkingDirectory = szWorkingDirectory

            My.Settings.TextEditorPath = txtTextEditorPath.Text.Trim()

            If (optStartupSizeMaximized.Checked) Then
                My.Settings.StartupOption = Constants.StartupOptions.MAXIMIZED
            Else
                My.Settings.StartupOption = Constants.StartupOptions.CUSTOM_SIZE
            End If

            If (NumericUpDownStartupSizeHeight.Enabled) Then My.Settings.StartupSizeHeight = NumericUpDownStartupSizeHeight.Value
            If (NumericUpDownStartupSizeWidth.Enabled) Then My.Settings.StartupSizeWidth = NumericUpDownStartupSizeWidth.Value

            My.Settings.Save()
            TabControlMain.SelectedIndex = 0
        Catch oExp As Exception
            Helper.DisplayException(oExp, Me, "Exception Saving Settings")
        End Try
    End Sub

    Private Sub pbSettingsCancel_Click(sender As Object, e As EventArgs) Handles pbSettingsCancel.Click
        Try
            ' Reload all settings
            LoadSettingsTabPage()
        Catch oExp As Exception
            Helper.DisplayException(oExp, Me, "Exception Changing Tabs")
        End Try
    End Sub

    Private Sub pbStartupSizeUseCurrent_Click(sender As Object, e As EventArgs) Handles pbStartupSizeUseCurrent.Click
        Try
            Dim iHeight As Integer = Height
            If (iHeight > NumericUpDownStartupSizeHeight.Maximum) Then
                iHeight = NumericUpDownStartupSizeHeight.Maximum
            ElseIf (iHeight < NumericUpDownStartupSizeHeight.Minimum) Then
                iHeight = NumericUpDownStartupSizeHeight.Minimum
            End If

            Dim iWidth As Integer = Width
            If (iWidth > NumericUpDownStartupSizeWidth.Maximum) Then
                iWidth = NumericUpDownStartupSizeWidth.Maximum
            ElseIf (iWidth < NumericUpDownStartupSizeWidth.Minimum) Then
                iWidth = NumericUpDownStartupSizeWidth.Minimum
            End If

            NumericUpDownStartupSizeHeight.Value = iHeight
            NumericUpDownStartupSizeWidth.Value = iWidth
        Catch oExp As Exception
            Helper.DisplayException(oExp, Me, "Exception Changing Tabs")
        End Try
    End Sub

    Private Sub frmMain_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        Try
            frmRevisionInfo.DisplayRevisionInfo(Me)
        Catch
        End Try
    End Sub

    Private Sub btnSetWorkingDirectory_Click(sender As Object, e As EventArgs) Handles btnSetWorkingDirectory.Click
        Try
            Using oDialog As FolderBrowserDialog = New FolderBrowserDialog()
                oDialog.RootFolder = Environment.SpecialFolder.LocalApplicationData
                oDialog.ShowNewFolderButton = True
                oDialog.Description = "Select the working directory for MRU"
                If (oDialog.ShowDialog(Me) = DialogResult.OK) Then
                    Dim szValue As String = Path.Combine(oDialog.SelectedPath, "MRU")
                    txtWorkingDirectory.Text = szValue
                End If
            End Using
        Catch oExp As Exception
            Helper.DisplayException(oExp, Me, "Exception Settings Working Directory For Application")
        End Try
    End Sub
End Class